package com.example.love_margin.Service.Impl;

import com.example.love_margin.Entity.Until.PageInfo;
import com.example.love_margin.Entity.User;
import com.example.love_margin.Mapper.SearchMapper;
import com.example.love_margin.Mapper.UserMapper;
import com.example.love_margin.Service.Impl.Until.delPetsUtil;
import com.example.love_margin.Service.PetService;
import com.example.love_margin.Service.UserService;
import com.example.love_margin.Untils.ResultCode;
import com.example.love_margin.Untils.forDelete;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    UserMapper userMapper;

    @Autowired
    SearchMapper searchMapper;

    @Autowired
    PetService petService;

    //批量删除实例化
    @Autowired
    delPetsUtil delP = new delPetsUtil();

    //创建删除批量文件的实例化
    @Autowired
    forDelete delete = new forDelete();

    @Override
    public Object deleteUser(int user_id) {
        if(userMapper.deleteUser(user_id)==1){
            try {
                //将此id下的所有的宠物都删除
                List<String> ii =  searchMapper.selectMySearchPet(user_id);
                delete.DeleteList(ii);
            }catch (Exception e){
                System.out.println(e);
            }

            return ResultCode.SUCCESS.result("删除成功");
        }
        return ResultCode.SUCCESS.result("删除失败");
    }

    @Override
    public Object selectOneUser(int user_id) {
        return ResultCode.SUCCESS.result(userMapper.selectOneUser(user_id));
    }

    @Override
    public Object selectUsers(Integer pageNum, Integer pageSize) {
        //用插件进行分页
        PageHelper.startPage(pageNum, pageSize);
        List<User> user = userMapper.selectUserList();
        // 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
        PageInfo<User> pageInfo = new PageInfo<>(user);
        return ResultCode.SUCCESS.result(pageInfo);
    }
}
